"use strict"

//全局配置插件
import {ipc} from "@/utils/ipcRenderer";
import {ipcApiRoute} from "@/api/main";
const syncConfChannel = 'app:SyncConfChannel'

export const globalConf = reactive({
    appTheme: 'white',
    appPrimaryColor: '#18a058',
    appPrimaryHoverColor: '#36ad6a',
    appPrimaryPressedColor: '#0c7a43',
    appPrimarySupplColor: '#2cb682',
    appMinorPrimaryColor: '#647eff',
    appZoomFactor: 1.0,
    appMainPinned: false,
    appMainIsMaximized: false,
    appAutoUpdates: 'sendnotify',
    _systemUpdaterForce: false,
    appCloseWindow: 'minimize',
    appLanguageI18n: 'zh-CN',
    appDownloadsPath: '',
    loginPowerOn: false
})
//初始化
const _init = () => {
    try {
        setGlobalConf(
            ipc.sendSync(ipcApiRoute['home.getAppConfig']) || {}
        )
    } catch (e) {
        console.error(e)
        ipc.send(ipcApiRoute['nativeBox.showBox'], {
            type: 'error',
            title: 'System Error',
            detail: e.toString() || 'Init configure failure.'
        })
    }
}
//订阅通道
const _subscribe = () => {
    const configListener = (event, args) => {
        setGlobalConf(args || {})
    }
    ipc.off(syncConfChannel, configListener)
    ipc.on(syncConfChannel, configListener)
}
//更新配置
export const setGlobalConf = (data = {}) => {
    for (let name in data) {
        globalConf[name] = data[name]
    }
}
const install = (app, option = {}) => {
    _init()
    _subscribe()
}
export default install